<?php
namespace cms\modules\backend\controllers;

use Yii;
use cms\controllers\AuthController;
use app\models\CoreConfig;
use app\models\BasicConfig;
use app\models\GlobalVariable;
use common\component\exception\ErrMap;
use common\component\tool\Tool;
use common\models\OperationLog;


class ConfigController extends AuthController
{
    /*核心设置*/
    public function actionCore()
    {
    	/*取出单个数据*/
        if(Yii::$app->request->isGet){
            $coreConfig = CoreConfig::find()
                            ->where('app_id = :id', [':id' => $this->app_id])->asArray()->one();
            // P($coreConfig);
            if(!is_null($coreConfig)){
                if(!empty($coreConfig['logo'])){
                    $coreConfig['logo'] = explode(',', $coreConfig['logo']);
                }
                // P($coreConfig);
                return Tool::showRes([0, '获取成功'], $coreConfig);
            }else{
                return Tool::showRes(ErrMap::getError(10010));
            };
        }


    	if(Yii::$app->request->isPost){
    		$coreConfig = new CoreConfig;
    		$res = $coreConfig->modifyConfig($this->post, $this->app_id);
    		if($res === true){
    			$backInfo = [0, '修改CMS实例核心配置信息成功'];//返回信息
    		}else if(gettype($res) == 'array'){
    			$backInfo = ErrMap::getError(10005);//返回信息
    			$exception = $res;
    		}
    	}

    	if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }




    /*基本配置设置*/
    public function actionBasic()
    {
        /*取出单个数据*/
        if(Yii::$app->request->isGet){
            $lang = isset($this->get['lang'])?$this->get['lang']:'zh';
            $basicConfig = BasicConfig::find()
                            ->where('app_id = :id and lang = :lang', [':id' => $this->app_id, ':lang' => $lang])->asArray()->one();
            // P($basicConfig);
            if(!is_null($basicConfig)){
                if(!empty($basicConfig['logo'])){
                    $basicConfig['logo'] = explode(',', $basicConfig['logo']);
                }
                // P($basicConfig);
                return Tool::showRes([0, '获取成功'], $basicConfig);
            }else{
                return Tool::showRes(ErrMap::getError(10010));
            };
        }

        if(Yii::$app->request->isPost){
            $basicConfig = new BasicConfig;
            $lang = isset($this->post['BasicConfig']['lang'])?$this->post['BasicConfig']['lang']:'zh';
            $res = $basicConfig->modifyConfig($this->post, $this->app_id, $lang);
            if($res === true){
                $backInfo = [0, '修改CMS实例基本配置信息成功'];//返回信息
            }else if(gettype($res) == 'array'){
                $backInfo = ErrMap::getError(10005);//返回信息
                $exception = $res;
            }
        }

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*全局变量列表*/
    public function actionGlobalVariableList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();

        $lang = isset($this->get['lang'])?$this->get['lang']:'zh';

        $globalVariable = new GlobalVariable;
        $count = $globalVariable::find()->where('app_id = :app_id', [':app_id' => $this->app_id])->count();
        $globalVariableList = $globalVariable::find()
                                ->where('app_id = :app_id and lang = :lang', [':app_id' => $this->app_id, ':lang' => $lang])
                                ->offset($pageSize*($currPage-1))->limit($pageSize)
                                ->asArray()->all();
        $res = array(
            'list' => $globalVariableList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*添加全局变量*/
    public function actionAddGlobalVariable()
    {
        if(Yii::$app->request->isPost){
            $globalVariable = new GlobalVariable;
            if($globalVariable->addVariable($this->post, $this->app_id)){
                $backInfo = [0, '添加APP全局变量成功'];//返回信息
            }else{
                if($globalVariable->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $globalVariable->getErrors();//异常信息
                    if(array_key_exists("variable_name_repeat", $exception)){
                        $backInfo = ErrMap::getError(10005, '此标签名称已存在');//返回信息
                    };
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*修改全局变量的值*/
    public function actionModifyGlobalVariable()
    {
        /*如果有数据，进行修改*/
        if(Yii::$app->request->isPost){
            $id = isset($this->post['GlobalVariable']['id'])?intval($this->post['GlobalVariable']['id']):'';
            if(!$id){
                $backInfo = ErrMap::getError(10005);//返回信息
            }else{
                $globalVariable = new GlobalVariable;
                if($globalVariable->modifyVariable($this->post, $id)){
                    $backInfo = [0, '修改全局变量成功'];//返回信息
                }else{
                    if($globalVariable->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $globalVariable->getErrors();//异常信息
                    }
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
                return $res;
            };

        }

        return Tool::showRes(ErrMap::getError(10005));
    }

    /*删除全局变量*/
    public function actionDelGlobalVariable()
    {
        $id = isset($this->get['id'])?$this->get['id']:'';
        if($id){
            $globalVariable = GlobalVariable::find()->where('id = :id', [':id' => $id])->one();
            if(!is_null($globalVariable)){
                if($globalVariable->sysval == 2){
                    $backInfo = ErrMap::getError(10013, '系统变量不能删除');//返回信息
                }else{
                    if($globalVariable->delete()){
                        $backInfo = [0, '删除全局变量成功'];//返回信息
                    }else{
                        $backInfo = ErrMap::getError(10013, '删除全局变量失败');//返回信息
                    }
                }
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), '') ) != 'boolean'){
                return $res;
            };

        }
        return Tool::showRes(ErrMap::getError(10005));

    }


}
